package com.un.ebs.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.un.ebs.core.UserContext;
import com.un.ebs.core.enums.ScopeEnum;
import com.un.ebs.core.enums.UserTypeEnum;
import com.un.ebs.core.exception.BusinessEntityNotFoundException;
import com.un.ebs.core.exception.BusinessPermissionDeniedException;
import com.un.ebs.core.util.SecurityUtils;
import com.un.ebs.sys.domain.DictionaryKey;
import com.un.ebs.sys.dto.DictionaryKeyAddDto;
import com.un.ebs.sys.dto.DictionaryKeyUpdateDto;
import com.un.ebs.sys.mapper.DictionaryKeyMapper;
import com.un.ebs.sys.service.DictionaryKeyService;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liubo
 * @since 2019-09-25
 */
@Service
public class DictionaryKeyServiceImpl extends ServiceImpl<DictionaryKeyMapper, DictionaryKey> implements DictionaryKeyService {

    @Override
    public void add(DictionaryKeyAddDto req) {
        DictionaryKey obj = new DictionaryKey();
        obj.setCode(req.getCode());
        obj.setName(req.getName());
        obj.setRemark(req.getRemark());
        obj.setScope(req.getScope());
        obj.setAppId(SecurityUtils.getUserContent().getAppId());
        obj.setIsDelete(false);
        obj.insert();
    }

    @Override
    public void update(DictionaryKeyUpdateDto req) {
        DictionaryKey obj = getById(req.getId());
        obj.setCode(req.getCode());
        obj.setName(req.getName());
        obj.setRemark(req.getRemark());
        obj.setScope(req.getScope());
        obj.updateById();
    }

    @Override
    public void delete(String id) {
        DictionaryKey dictionaryKey = getBaseMapper().selectById(id);
        if (dictionaryKey == null) {
            throw new BusinessEntityNotFoundException(id, "字典");
        }

        UserContext userContext = SecurityUtils.getUserContent();
        if (dictionaryKey.getScope() == ScopeEnum.Application) {
            if (userContext.getType() == UserTypeEnum.DEVELOPER && userContext.getAppId().equals(dictionaryKey.getAppId())) {
                dictionaryKey.setIsDelete(true);
            } else {
                throw new BusinessPermissionDeniedException(String.format("{0} 字典的范围值是 {1} ，而你的用户类型与字典范围不匹配。",
                        dictionaryKey.getName(),
                        dictionaryKey.getScope().toString()));
            }
        } else {
            dictionaryKey.setIsDelete(true);
        }
        dictionaryKey.updateById();
    }

    @Override
    public DictionaryKey queryById(String id) {
        return getBaseMapper().selectById(id);
    }

    @Override
    public DictionaryKey queryByKey(String key) {
        LoggerFactory.getLogger(this.getClass()).warn(String.format("查询字典【%s】，userContext : %s", key, SecurityUtils.getUserContent()));
        LambdaQueryWrapper<DictionaryKey> lq = Wrappers.lambdaQuery();
        lq.eq(DictionaryKey::getCode, key);
        lq.eq(DictionaryKey::getAppId, SecurityUtils.getUserContent().getAppId());
        return baseMapper.selectOne(lq);
    }
}